﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BonSaiTableAdapters;
public partial class admin_NewDetails : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack) 
        {
            if (Request.QueryString["newID"] != null)
            {
                try
                {
                    txtDateCreate.Text = DateTime.Now.ToShortDateString();
                    int newID = Int32.Parse(Request.QueryString["newID"].ToString());
                    ViewState["newID"] = newID;
                    LoadNewInfo();

                }
                catch (Exception ex)
                {

                }
            }
            else
            {
                btnUpdate.Text = "Tạo mới";
                btnDelete.Visible = false;
            }
        }
    }

    private void LoadNewInfo()
    {
        if (ViewState["newID"] != null)
        {
            int newID = Int32.Parse(ViewState["newID"].ToString());
            NewsTableAdapter newDA = new NewsTableAdapter();
            BonSai.NewsDataTable newDT = newDA.GetNewByID(newID);
            if (newDT.Rows.Count > 0)
            {
                BonSai.NewsRow newRow = newDT.Rows[0] as BonSai.NewsRow;
                txtNewID.Text = newRow.NewID.ToString();
                if (!newRow.IsTitleNull())
                    txtTitle.Text = newRow.Title;
                if (!newRow.IsBriefDescNull())
                    txtBriefDesc.Text = newRow.BriefDesc;
                if (!newRow.IsContentNull())
                    fckBrief.Value = newRow.Content;
                if (!newRow.IsDateCreatedNull())
                    txtDateCreate.Text = newRow.DateCreated.ToShortDateString();

                hlRepresentImage.Visible = true;
                hlRepresentImage.NavigateUrl = "New_RepresentImage.aspx?newID=" + newID;
                if (!newRow.IsRepresentImageNull()) 
                {
                    newRepresentImage.ImageUrl = "~/img_news/" + newRow.RepresentImage;
                }
            }

        }
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("NewsManage.aspx");
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
         if (!Page.IsValid)
            return;

        string title = txtTitle.Text.Trim();
        string briefDesc = txtBriefDesc.Text.Trim();
        string content = fckBrief.Value;
        bool status = true;
        

        NewsTableAdapter newDA = new NewsTableAdapter();
        if (ViewState["newID"] != null)
        {
            ///UpdateNews
            int newID = Int32.Parse(ViewState["newID"].ToString());
            newDA.Update(title,briefDesc,content,DateTime.Now,"",status,newID);
            Response.Redirect("NewsManage.aspx?code=2");
        }
        else
        { 
            //Add New
            newDA.Insert(title,briefDesc,content,DateTime.Now,"",status);
            Response.Redirect("NewsManage.aspx?code = 1");
        }
        
    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        if (ViewState["newID"] != null)
        {
            int newID = Int32.Parse(ViewState["newID"].ToString());
            NewsTableAdapter newDA = new NewsTableAdapter();
            newDA.Delete(newID);
            Response.Redirect("ProductManage.aspx?code=3");
            }
    }
 }


